package com.bdqn.pojo;

import jakarta.persistence.*;

import java.io.Serial;
import java.io.Serializable;

/**
 * 用户（数据库表对应的实体类）
 *
 * @author LILIBO
 * @since 2023-11-27
 */
// @Data
// @NoArgsConstructor
// @AllArgsConstructor
// @ToString
@Entity // 表示是一个JPA规范的实体类
@Table(name = "sys_user") // 对应数据库中的表名称
@NamedQueries(@NamedQuery(name = "User.findUsersByName", query = "select u from User u where u.usrName = ?1"))
public class User implements Serializable {

    @Serial
    private static final long serialVersionUID = 42L;

    @Id // 表示该字段是主键
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 主键生成策略，ID序列
    @Column(name = "usr_id") // 表示该属性对应数据库中的字段
    private Long usrId; // 实体类中的属性

    @Column(name = "usr_name")
    private String usrName;

    @Column(name = "usr_password")
    private String usrPassword;

    /*
     * 通过多对一关系映射（屏蔽此代码）
     */
    /* @Column(name = "usr_role_id")
    private Long usrRoleId; */

    /**
     * 用户所属角色（通过多对一关联）
     */
    @ManyToOne(targetEntity = Role.class) // 多对一关联关系
    @JoinColumn(name = "usr_role_id") // 外键关联字段
    private Role role;

    @Column(name = "usr_flag")
    private Long usrFlag;

    public User() {
    }

    public User(String usrName, String usrPassword, Role role, Long usrFlag) {
        this.usrName = usrName;
        this.usrPassword = usrPassword;
        this.role = role;
        this.usrFlag = usrFlag;
    }

    public User(Long usrId, String usrName, String usrPassword, Role role, Long usrFlag) {
        this.usrId = usrId;
        this.usrName = usrName;
        this.usrPassword = usrPassword;
        this.role = role;
        this.usrFlag = usrFlag;
    }

    @Override
    public String toString() {
        return "User{" +
                "usrId=" + usrId +
                ", usrName='" + usrName + '\'' +
                ", usrPassword='" + usrPassword + '\'' +
                ", roleName=" + role.getRoleName() + // 解决循环打印toString()错误
                ", usrFlag=" + usrFlag +
                '}';
    }

    public Long getUsrId() {
        return usrId;
    }

    public void setUsrId(Long usrId) {
        this.usrId = usrId;
    }

    public String getUsrName() {
        return usrName;
    }

    public void setUsrName(String usrName) {
        this.usrName = usrName;
    }

    public String getUsrPassword() {
        return usrPassword;
    }

    public void setUsrPassword(String usrPassword) {
        this.usrPassword = usrPassword;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Long getUsrFlag() {
        return usrFlag;
    }

    public void setUsrFlag(Long usrFlag) {
        this.usrFlag = usrFlag;
    }

}
